package com.lansi.fb.person.controller;

import java.io.File;
import java.net.InetAddress;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lansi.bean.ResponseResult;
import com.lansi.fb.person.bean.Person;
import com.lansi.fb.person.service.IPersonService;



@Controller
@RequestMapping("/chemical")
public class PersonController {
	//依赖注入
	@Resource
	private IPersonService personService;
	
	//处理人员信息注册
	@RequestMapping("/registerPerson.do")
	@ResponseBody
	public ResponseResult<Void> RegisterPerson(
			/* String  username,
			 String  sex,
			 Integer age,
			 String  worktype,
			 String  sn,
			 String  level,
			 String  cardid,
			 String  userid,
			 String  picture,
			 String  memo*/
			 @RequestBody Map<String,Object> map
	
			){
		String  username =null;
		String  sex      =null;
		Integer age      =null;
		String  worktype =null;
		String  sn       =null;
		String  level	 =null;
		String  cardid	 =null;
		String  userid   =null;
		String  picture  =null;
		String  memo     =null;
		System.out.println("人员信息注册:register.do");
		ResponseResult<Void> rr = new ResponseResult<Void>();		
				
		try {
			
			  username =(String) map.get("username");
			  sex      =(String) map.get("sex");
			  age      =(Integer) map.get("age");
			  worktype =(String) map.get("worktype");
			  sn       =(String) map.get("sn");
			  level	   =(String) map.get("level");
			  cardid   =(String) map.get("cardid");
			  userid   =(String) map.get("userid");
			  picture  =(String) map.get("picture");
			  memo     =(String) map.get("memo");
			List<Person> list =  personService.getPersonByUserid(userid);
			if(list.isEmpty()) {
				Person person =new Person();
				person.setUsername(username);
				person.setSex(sex);
				person.setAge(age);
				person.setWorktype(worktype);
				person.setSn(sn);
				person.setLevel(level);
				person.setCardid(cardid);
				person.setUserid(userid);
				person.setPicture(picture);
				person.setMemo(memo);
				person.setCtime(new Date());			
				personService.register(person);
				rr.setState(1);
				rr.setMessage("注册成功");
			}else {		
				rr.setState(1);
				rr.setMessage("注册失败/用户已存在");
			}
	 		
			
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}

		return rr;		
	}
	
	//处理批量删除人员信息和单个删除人员信息
	@RequestMapping("/delPerson.do")
	@ResponseBody
	public ResponseResult<Void> delPerson(/*Integer[] ids */  @RequestBody Map<String,Integer[]> map) {
		Integer[] ids      =null;
		ResponseResult<Void> rr = new ResponseResult<Void>();
		try {
		    ids      =map.get("ids");
			System.out.println("删除人员个数:"+ids.length);
			if(ids.length>1) {
				personService.delPerson(ids);
				rr.setState(1);
				rr.setMessage("删除成功");
			}else {
				Integer id = ids[0];
				personService.delById(id);
				rr.setState(1);
				rr.setMessage("删除成功");
			}
			
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}

		return rr;
	}	
	
	//修改用户信息
	@RequestMapping("/updateByPerson.do")
	@ResponseBody
	public ResponseResult<Void> updatePerson(
			/* Integer id,
			 String  username,
			 String  sex,
			 Integer age,
			 String  worktype,
			 String  sn,
			 String  level,
			 String  cardid,
			 String  userid,
			 String  picture,
			 String  memo*/	
			 @RequestBody Map<String,Object> map
			){
		  System.out.println("修改个人信息:updateByPerson.do");
		    Integer id       =null;
		    String  username =null;
		    String  sex      =null;
			Integer age      =null;
			String  worktype =null;
			String  sn       =null;
			String  level	 =null;
			String  cardid	 =null;
			String  userid   =null;
			String  picture  =null;
			String  memo     =null;
		 ResponseResult<Void> rr = new  ResponseResult<Void>();				
			 try {
				      id       =(Integer) map.get("id");
				      username =(String) map.get("username");
					  sex      =(String) map.get("sex");
					  age      =(Integer) map.get("age");
					  worktype =(String) map.get("worktype");
					  sn       =(String) map.get("sn");
					  level	   =(String) map.get("level");
					  cardid   =(String) map.get("cardid");
					  userid   =(String) map.get("userid");
					  picture  =(String) map.get("picture");
					  memo     =(String) map.get("memo");
				Date ctime =new Date(); 		
				personService.updatePerson(id, username, sex, age, worktype, sn, level, cardid, userid, picture, memo, ctime);
				rr.setState(1);
				rr.setMessage("修改成功");
			} catch (RuntimeException e) {
				rr.setState(-1);
				rr.setMessage(e.getMessage());
			}		
			return rr;
				
			}
	
	//查询出所有的用户信息
	@RequestMapping("/getByPerson.do")
	@ResponseBody
	public ResponseResult<List<Person>> getPerson(
			/*Integer page,
			String username,
			String sex,
			String level,
			String cardid,
			Integer age,
			String worktype,
			String sn,
			String userid*/
			@RequestBody Map<String,Object> map
			){
		Integer page     =null;
	    String  username =null;
	    String sex      =null;
		Integer age      =null;
		String worktype =null;
		String  sn       =null;
		String level	 =null;
		String  cardid	 =null;
		String  userid   =null;
		Integer  fbsex  =null;
		Integer  fbworktype=null;
		Integer  fblevel =null;
		ResponseResult<List<Person>> rr = new ResponseResult<List<Person>>();
		try {
			
		      page     =(Integer) map.get("page");                             
		      username =(String) map.get("username");
		      
			  sex      =(String) map.get("sex");	//传过来sex是string类型---先判断是否是 ""空字符串   不是空字符串在进行类型转换		  
			  if(sex!=null && !"".equals(sex)) {   
				    fbsex=Integer.valueOf(sex);
			  }
			  age      =(Integer) map.get("age");  
			  
			  worktype =(String) map.get("worktype");	//传过来worktype是string类型---先判断是否是 ""空字符串   不是空字符串在进行类型转换		  
			  if(worktype!=null  && !"".equals(worktype)) {
				    fbworktype=Integer.valueOf(worktype);
			  }
			  sn       =(String) map.get("sn");
			  
			  level	   =(String) map.get("level");		//传过来level是string类型---先判断是否是 ""空字符串   不是空字符串在进行类型转换	  
			  if(level!=null  &&  !"".equals(level)) {
				   fblevel = Integer.valueOf(level);
			  }
			  cardid   =(String) map.get("cardid");
			  userid   =(String) map.get("userid");
										
			Integer Personcount = personService.getPersoncount(username, fbsex, fblevel, cardid, age, fbworktype, sn, userid);//数据库用户数量
			if(page==null) {
				page=1;
			}
			Integer offset = (page-1)*10;//跳过的页数
			List<Person> list=personService.getPerson(username, fbsex, fblevel, cardid, age, fbworktype, sn, userid, offset, 10);
			System.out.println(list);
			rr.setData(list);
			rr.setState(1);
			rr.setMessage(Personcount.toString());
		} catch (RuntimeException e) {
			rr.setMessage(e.getMessage());
			rr.setState(-1);			
		}		
		return rr;		
	}	
	
	//上传图片
	@RequestMapping("/uploadpicture.do")
	@ResponseBody
	public ResponseResult<Void> upload(
			@RequestParam("file")  MultipartFile file,
			 String userid) throws Exception{
		
		ResponseResult<Void> rr = new ResponseResult<Void>();
		try {			
			String ip= InetAddress.getLocalHost().getHostAddress();//获取IP地址
			System.out.println("你的IP地址是："+ip);
			String path=null;
			if("192.168.2.14".equals(ip)) {
				 path ="E:/usr/local/picture";
				System.out.println(path);
			}else {
				 path ="/usr/local/picture";
				System.out.println(path);
			}
			System.out.println(file.getOriginalFilename());
			//上传
			file.transferTo(new File(path,file.getOriginalFilename()));
			//保存头像
			personService.updateImage(file.getOriginalFilename(),userid);
			//更新session中的user对象
			/*session.setAttribute("user",personService.getPersonById(id));*/
			rr.setState(1);
			rr.setMessage("上传图片成功");
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}
		
		return rr;
	}
	

}
